<template>
  <div class="app-container">
    <!--
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          &lt;!&ndash;      <el-form-item label="加注站id编号" prop="stationId">&ndash;&gt;
          &lt;!&ndash;        <el-input&ndash;&gt;
          &lt;!&ndash;          v-model="queryParams.stationId"&ndash;&gt;
          &lt;!&ndash;          placeholder="请输入加注站id编号"&ndash;&gt;
          &lt;!&ndash;          clearable&ndash;&gt;
          &lt;!&ndash;          @keyup.enter.native="handleQuery"&ndash;&gt;
          &lt;!&ndash;        />&ndash;&gt;
          &lt;!&ndash;      </el-form-item>&ndash;&gt;
          <el-form-item label="设备名称" prop="facilitName">
            <el-input
              v-model="queryParams.facilitName"
              placeholder="请输入设备名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="设备维护人员姓名" prop="facilitUsername">
            <el-input
              v-model="queryParams.facilitUsername"
              placeholder="请输入设备维护人员姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="设备唯一标识sn" prop="facilitSn">
            <el-input
              v-model="queryParams.facilitSn"
              placeholder="请输入设备唯一标识sn"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="设备加注单价(元)" prop="unitTemporary">
            <el-input
              v-model="queryParams.unitTemporary"
              placeholder="请输入设备加注单价(元)"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="设备余量(kg)" prop="remainingLiquid">
            <el-input
              v-model="queryParams.remainingLiquid"
              placeholder="请输入设备余量(kg)"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="设备满库量(kg)" prop="totalLiquid">
            <el-input
              v-model="queryParams.totalLiquid"
              placeholder="请输入设备满库量(kg)"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="经度" prop="facilitLat">
            <el-input
              v-model="queryParams.facilitLat"
              placeholder="请输入经度"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="纬度" prop="facilitLng">
            <el-input
              v-model="queryParams.facilitLng"
              placeholder="请输入纬度"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="加注机所属省" prop="facilitProvince">
            <el-input
              v-model="queryParams.facilitProvince"
              placeholder="请输入加注机所属省"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="加注机所属市" prop="facilitCity">
            <el-input
              v-model="queryParams.facilitCity"
              placeholder="请输入加注机所属市"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="加注机所属区县" prop="facilitArea">
            <el-input
              v-model="queryParams.facilitArea"
              placeholder="请输入加注机所属区县"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="剩余天数" prop="facilitExpire">
            <el-input
              v-model="queryParams.facilitExpire"
              placeholder="请输入剩余天数"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="机器模式" prop="facilitPattern">
            <el-select v-model="queryParams.facilitPattern" placeholder="请选择" clearable>
              <el-option
                v-for="dict in dict.type.c_facilit_pattern"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          &lt;!&ndash;            <el-form-item label="是否删除" prop="isDelete">
                        <el-input
                          v-model="queryParams.isDelete"
                          placeholder="请输入是否删除"
                          clearable
                          @keyup.enter.native="handleQuery"
                        />
                      </el-form-item>&ndash;&gt;
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
    -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="selectCFillingFacilitList"
          v-hasRole="['dragon', 'admin']"
        >添加加注机
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasRole="['dragon', 'admin']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasRole="['dragon', 'admin']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasRole="['dragon', 'admin']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="facilitList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="加注站id编号" align="center" prop="stationId"/>
      <el-table-column label="设备名称" align="center" prop="facilitName"/>
      <el-table-column label="设备维护人员姓名" align="center" prop="facilitUsername"/>
      <el-table-column label="设备唯一标识sn" align="center" prop="facilitSn"/>
      <el-table-column label="设备加注单价(元)" align="center" prop="unitTemporary">
        <template slot-scope="scope">
          {{ fenToYuan(scope.row.unitTemporary) }}
        </template>
      </el-table-column>
      <el-table-column label="设备余量(kg)" align="center" prop="remainingLiquid">
        <template slot-scope="scope">
          {{ mlToL(scope.row.remainingLiquid) }}
        </template>
      </el-table-column>
      <el-table-column label="设备满库量(kg)" align="center" prop="totalLiquid">
        <template slot-scope="scope">
          {{ mlToL(scope.row.totalLiquid) }}
        </template>
      </el-table-column>
      <el-table-column label="设备流量系数" align="center" prop="faciltKvs">
        <template slot-scope="scope">
          {{ scope.row.faciltKvs }}
        </template>
      </el-table-column>
      <el-table-column label="设备二维码" align="center" prop="facilitImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.facilitImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="经度" align="center" prop="facilitLat"/>
            <el-table-column label="纬度" align="center" prop="facilitLng"/>
            <el-table-column label="加注机所属省" align="center" prop="facilitProvince"/>
            <el-table-column label="加注机所属市" align="center" prop="facilitCity"/>
            <el-table-column label="加注机所属区县" align="center" prop="facilitArea"/>-->
      <el-table-column label="剩余天数" align="center" prop="facilitExpire"/>
      <el-table-column label="机器模式" align="center" prop="facilitPattern">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_facilit_pattern" :value="scope.row.facilitPattern"/>
        </template>

      </el-table-column>
      <el-table-column label="状态，在线 0" online
      ", 离线 1"offline", 加注中 2"inuse"" align="center" prop="status" />
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template style="display: flex" slot-scope="scope">
          <!--                    <el-button
                                size="mini"
                                type="text"
                                icon="el-icon-edit"
                                @click="handleUpdate(scope.row)"
                                v-has-role="['dragon', 'admin']"
                              >修改
                              </el-button>-->

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-has-role="['dragon', 'admin']"
          >参数调整
          </el-button>

          <el-select v-model="form.facilitPattern" @change="updateStationMode(scope.row.facilitId)"
                     placeholder="机器模式">
            <el-option
              v-for="dict in dict.type.c_facilit_pattern"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"

            ></el-option>
          </el-select>


          <!--          <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="fullStorage(scope.row)"
                      v-has-role="['dragon', 'admin']"
                    >一键满库
                    </el-button>-->
          <!--          <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                      v-has-role="['dragon', 'admin']"
                    >删除
                    </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改加注机对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-table v-if="form.stationId===null" v-loading="loading" :data="unFacilitList"
                @selection-change="handleSelectionChange">
        <el-table-column label="加注机名" align="center" prop="facilitName"/>
        <el-table-column label="加注机sn" align="center" prop="facilitSn"/>
        <el-table-column label="加注机二维码" align="center" prop="facilitImg" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.facilitImg" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <!--        <el-table-column label="用户姓名" align="center" prop="userName"/>-->
        <!--      <el-table-column label="是否删除" align="center" prop="isDelete" />-->
        <!--      <el-table-column label="状态，0：正常，1：禁用" align="center" prop="status" />-->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-plus"
              @click="bindFacilit(scope.row)"
            >绑定
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-form v-if="form.stationId!=null" ref="form" :model="form" :rules="rules" label-width="80px">
        <!--        <el-form-item label="加注站id编号" prop="stationId">
                  <el-input v-model="form.stationId" placeholder="请输入加注站id编号"/>
                </el-form-item>-->
        <el-form-item label="设备名称" prop="facilitName">
          <el-input v-model="form.facilitName" placeholder="请输入设备名称"/>
        </el-form-item>
        <el-form-item label="设备维护人员姓名" prop="facilitUsername">
          <el-input v-model="form.facilitUsername" placeholder="请输入设备维护人员姓名"/>
        </el-form-item>
        <el-form-item label="设备唯一标识sn" prop="facilitSn">
          <el-input disabled v-model="form.facilitSn" placeholder="请输入设备唯一标识sn"/>
        </el-form-item>
        <el-form-item label="设备加注单价(元)" prop="unitTemporary">
          <el-input v-model="unitTemporary" placeholder="请输入设备加注单价(元)" @blur="handleBlurUnitTemporary"/>
        </el-form-item>
        <el-form-item label="设备余量(kg)" prop="remainingLiquid">
          <el-input v-model="remainingLiquid" placeholder="请输入设备余量(kg)" @blur="handleBlurRemainingLiquid"/>
        </el-form-item>
        <el-form-item label="设备满库量(kg)" prop="totalLiquid">
          <el-input v-model="totalLiquid" placeholder="请输入设备满库量(kg)" @blur="handleBlurTotalLiquid"/>
        </el-form-item>
        <el-form-item label="设备流量系数" prop="faciltKvs">
          <el-input v-model="form.faciltKvs" placeholder="请输入设备流量系数"/>
        </el-form-item>
        <el-form-item label="设备二维码" prop="facilitImg">
          <image-upload disabled v-model="form.facilitImg"/>
        </el-form-item>
        <!--        <el-form-item label="经度" prop="facilitLat">
                  <el-input disabled v-model="form.facilitLat" placeholder="请输入经度"/>
                </el-form-item>
                <el-form-item label="纬度" prop="facilitLng">
                  <el-input disabled v-model="form.facilitLng" placeholder="请输入纬度"/>
                </el-form-item>
                <el-form-item label="加注机所属省" prop="facilitProvince">
                  <el-input disabled v-model="form.facilitProvince" placeholder="请输入加注机所属省"/>
                </el-form-item>
                <el-form-item label="加注机所属市" prop="facilitCity">
                  <el-input disabled v-model="form.facilitCity" placeholder="请输入加注机所属市"/>
                </el-form-item>
                <el-form-item label="加注机所属区县" prop="facilitArea">
                  <el-input disabled v-model="form.facilitArea" placeholder="请输入加注机所属区县"/>
                </el-form-item>-->
        <!--        <el-form-item label="剩余天数" prop="facilitExpire">
                  <el-input v-model="form.facilitExpire" placeholder="请输入剩余天数"/>
                </el-form-item>-->
        <!--        <el-form-item label="机器模式" prop="userRole">
                  <el-select v-model="form.facilitPattern" placeholder="请选择机器模式">
                    <el-option
                      v-for="dict in dict.type.c_facilit_pattern"
                      :key="dict.value"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                    ></el-option>
                  </el-select>
                </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateStationStatus">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listFacilit, getFacilit, delFacilit, addFacilit, updateFacilit} from "@/api/consumer/facilit";
import {
  updateStationStatus,
  selectCFillingFacilitList,
  bindFacilit,
  updateFacilitMode
} from "@/api/consumer/admin/station";
import {fenToYuan, mlToL} from "@/utils/unitUtil";

export default {
  name: "Facilit",
  dicts: ['c_facilit_pattern'],
  props: {
    exampleProp: Number,
  },
  watch: {
    exampleProp(newValue) {
      // 调用子组件的 getList 方法
      this.getList();
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 加注机表格数据
      facilitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stationId: null,
        facilitName: null,
        facilitUsername: null,
        facilitSn: null,
        unitTemporary: null,
        remainingLiquid: null,
        totalLiquid: null,
        faciltKvs: null,
        facilitImg: null,
        facilitLat: null,
        facilitLng: null,
        facilitProvince: null,
        facilitCity: null,
        facilitArea: null,
        facilitExpire: null,
        facilitPattern: null,
        isDelete: null,
        status: 1
      },
      // 表单参数
      form: {
        stationId: null,
      },
      unitTemporary: null,
      remainingLiquid: null,
      totalLiquid: null,
      // 表单校验
      rules: {
        faciltKvs: [
          {required: true, message: "请输入设备流量系数", trigger: "blur"},
          // {pattern: /^[0-9]*$/, message: "请输入数字", trigger: "blur"},
        ],
      },
      // 未绑定加注机列表
      unFacilitList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    fenToYuan,
    mlToL,
    // 单价移开时处理
    handleBlurUnitTemporary() {
      this.form.unitTemporary = Math.round(this.unitTemporary * 100);
    },
    // 设备余量移开时处理
    handleBlurRemainingLiquid() {
      this.form.remainingLiquid = Math.round(this.remainingLiquid * 1000);
    },
    // 设备满库量移开时处理
    handleBlurTotalLiquid() {
      this.form.totalLiquid = Math.round(this.totalLiquid * 1000);
    },
    //修改加注机参数
    updateStationStatus() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.facilitId != null) {
            updateStationStatus(this.form).then(response => {
              console.log("response", response);
              if (response.msg === "修改机器状态成功") {
                this.$modal.msgSuccess("修改机器状态成功");
              } else if (response.msg === "加注机不处于待机状态,无法修改,请重试") {
                this.$modal.msgError("加注机不处于待机状态,无法修改,请重试");
              }
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //修改加注机模式
    updateStationMode(methead) {
      console.log(this.form.facilitPattern, methead)
      updateFacilitMode(this.form.facilitPattern, methead).then(response => {
        console.log("response", response);
        if (response.msg === "修改机器模式成功") {
          this.$modal.msgSuccess("修改机器模式成功");
        } else {
          this.$modal.msgError(response.msg);
        }
        this.open = false;
        this.getList();
      });
    },

    //查看未添加的加注机
    selectCFillingFacilitList() {
      this.reset();
      // this.form.stationId = this.exampleProp;
      this.open = true;
      this.title = "添加加注机";
      selectCFillingFacilitList(this.queryParams).then(response => {
        console.log("查看未绑定的加注机", response)
        this.unFacilitList = response.data;
        this.loading = false;
      });
    },
    //绑定加注机到加注站
    bindFacilit(methead) {
      console.log("methead", methead.facilitId, this.exampleProp)
      bindFacilit(this.exampleProp, methead.facilitId).then(response => {
        console.log("绑定加注机到加注站", response)
        if (response.msg == "绑定成功") {
          this.$modal.msgSuccess("绑定成功");
          this.open = false;
          this.getList();
        } else {
          this.$modal.msgError("绑定失败");
        }
        this.getList()
      });
    },

    /** 查询加注机列表 */
    getList() {
      this.loading = true;
      this.queryParams.stationId = this.exampleProp;
      this.queryParams.status = 1
      listFacilit(this.queryParams).then(response => {
        this.facilitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.unitTemporary = null;
      this.form = {
        facilitId: null,
        stationId: null,
        facilitName: null,
        facilitUsername: null,
        facilitSn: null,
        unitTemporary: null,
        remainingLiquid: null,
        totalLiquid: null,
        faciltKvs: null,
        facilitImg: null,
        facilitLat: null,
        facilitLng: null,
        facilitProvince: null,
        facilitCity: null,
        facilitArea: null,
        facilitExpire: null,
        facilitPattern: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.facilitId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.stationId = this.exampleProp;
      this.open = true;
      this.title = "添加加注机";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const facilitId = row.facilitId || this.ids
      getFacilit(facilitId).then(response => {
        this.form = response.data;
        this.unitTemporary = fenToYuan(this.form.unitTemporary);
        this.remainingLiquid = mlToL(this.form.remainingLiquid);
        this.totalLiquid = mlToL(this.form.totalLiquid);
        this.open = true;
        this.title = "修改加注机";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.facilitId != null) {
            console.log("修改加注机", this.form)
            updateFacilit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFacilit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const facilitIds = row.facilitId || this.ids;
      this.$modal.confirm('是否确认删除加注机编号为"' + facilitIds + '"的数据项？').then(function () {
        return delFacilit(facilitIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('consumer/facilit/export', {
        ...this.queryParams
      }, `facilit_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
